var myFolderList=null;
var pagin=null;
var pageNum=null;
var totalPage=null;


$(function (){
    myFolderList=$(".tablelist");
    pagin=$(".pagin");
    pageNum=1;

    //初始化页面去拿取我的文件夹的列表
    getMyFolderList();

})



//分页拿取数据
function fy(p,totalpage) {
    totalPage=parseInt(totalpage);
    pageNum=parseInt(p);
    if(p>totalPage){
        alert("最后一页了")
    }
    else if(p<1) {
        alert("前面已经没有了")
    }else {
        getMyFolderList();
    }
}



//拿取数据的方法
function getMyFolderList() {
    $.ajax({
        url: "/sharedFolder/myFolderList",
        data: {"pageNum":pageNum},
        dataType: "json",
        type: "post",
        success:function (result){
            if(result.data!=null){
                var str="    \t<thead>\n" +
                    "    \t<tr>\n" +
                    "        <th>名称</th>\n" +
                    "        <th>修改日期</th>\n" +
                    "        <th>大小</th>\n" +
                    "        <th>创建人</th>\n" +
                    "        <th style=\"width: 180px\">操作</th>\n" +
                    "        </tr>\n" +
                    "        </thead>\n" +
                    "        \n" +
                    "        <tbody>";
                for (let i = 0; i < result.data.length; i++) {
                    str+="<tr>\n" +
                        "        <td style=\"height: 80px\"><img src='../../../static/images/folders.jpeg'>&nbsp;&nbsp;"+result.data[i].folderName+"</td>\n" +
                        "        <td>"+result.data[i].folderModifytime+"</td>\n"+
                        "        <td>"+result.data[i].folderSize+"B</td>\n" +
                        "        <td>"+result.data[i].empName+"</td>\n" +
                        "        <td>&nbsp;&nbsp;<a href=\"#\" class=\"query\" onclick='query("+result.data[i].folderId+")'>查看</a> &nbsp;&nbsp;&nbsp;&nbsp;  <a href=\"deptUpdate.html\" class=\"add\" onclick='add("+result.data[i].folderId+")'>新建</a> &nbsp;&nbsp;&nbsp;&nbsp;  <a href=\"deptUpdate.html\" class=\"modify\" onclick='modify("+result.data[i].folderId+")'>修改</a> &nbsp;&nbsp;&nbsp;&nbsp;  <a href=\"#\" class=\"delete\" onclick='dete("+result.data[i].folderId+")'>删除</a></td>\n" +
                        "        </tr>";
                }
                str+="        </tbody>";
                myFolderList.html(str);


                var str1="<div class=\"message\">共<i class=\"blue\">"+result.totalRows+"</i>条记录，当前显示第&nbsp;<i class=\"blue\">"+result.pageNum+"&nbsp;</i>页</div>\n" +
                "      <ul class=\"paginList\">\n" +
                "        <li class=\"paginItem\"><a href=\"javascript:;\" onclick='fy("+(parseInt(result.pageNum)-1)+","+result.totalPage+")'><span class=\"pagepre\"></span></a></li>\n" ;
                if(result.totalPage>3){
                    str1+="                <li class=\"paginItem\"><a href=\"javascript:;\" onclick='fy(1)'>1</a></li>\n" +
                        "                <li class=\"paginItem more\"><a href=\"javascript:;\" >...</a></li>\n" +
                        "                <li class=\"paginItem\"><a href=\"javascript:;\" onclick='fy("+result.totalPage+")'>"+result.totalPage+"</a></li>\n" +
                        "                <li class=\"paginItem\"><a href=\"javascript:;\" onclick='fy("+(parseInt(result.pageNum)+1)+","+result.totalPage+")'><span class=\"pagenxt\"></span></a></li>\n" +
                        "            </ul>";
                }else {
                    for (let i = 1; i <=result.totalPage; i++) {
                        str1+="                <li class=\"paginItem\"><a href=\"javascript:;\" onclick='fy("+i+")'>"+i+"</a></li>\n";
                    }
                    str1+= "                <li class=\"paginItem\"><a href=\"javascript:;\" onclick='fy("+(parseInt(result.pageNum)+1)+","+result.totalPage+")'><span class=\"pagenxt\"></span></a></li>\n" +
                        "            </ul>";
                }

                pagin.html(str1);

            }else {
                alert('您目前没有文件夹喔');
                //跳到新建文件夹页面
                //
            }
        }

    })
}


function query(folderId) {
    $.ajax({
        url: "/sharedFolder/goMyFolderView",
        data: {"folderId":folderId},
        dataType: "json",
        type: "post",
        success: function (result) {
            if(result){
                location.href="goView";
            }else {
                // location.href="../../../../templates/error.html";
            }
        }
    })
}



function add(folderId) {
    $.ajax({
        url: "/sharedFolder/goMyFolderAddView",
        data: {"folderId":folderId},
        dataType: "json",
        type: "post",
        success: function (result) {
            if(result){
                location.href="goAddView";
            }else {
                // location.href="../../../../templates/error.html";
            }
        }
    })
}


function modify(folderId) {
    $.ajax({
        url: "/sharedFolder/goMyFolderModifyView",
        data: {"folderId":folderId},
        dataType: "json",
        type: "post",
        success: function (result) {
            if(result){
                location.href="goModifyView";
            }else {
                // location.href="../../../../templates/error.html";
            }
        }
    })
}



function dete(folderId) {
    var b = confirm("您确定要删除吗？");
    if(b){
        $.ajax({
            url: "/sharedFolder/deleteMyFolder",
            data: {"folderId":folderId},
            dataType: "json",
            type: "post",
            success: function (result) {
                if(result){
                    alert("删除成功")
                    location.reload();
                }else {
                    // location.href="../../../../templates/error.html";
                }
            }
        })
    }

}



